package test2constructor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigUtils;
import org.springframework.context.support.GenericApplicationContext;

import javax.inject.Provider;


/**
 * @author: mornd
 * @dateTime: 2023/5/17 - 21:18
 * javax.inject.Provider 解决构造器产生的循环依赖
 */
@Slf4j
public class Demo3 {
    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();
        context.registerBean("aaa", AAA.class);
        context.registerBean("bbb", BBB.class);
        AnnotationConfigUtils.registerAnnotationConfigProcessors(context.getDefaultListableBeanFactory());
        context.refresh();
        AAA aaa = context.getBean(AAA.class);
        BBB bbb = context.getBean(BBB.class);
        System.out.println("-------------------------");
        System.out.println(aaa.getClass());
        System.out.println(bbb.getClass());
        bbb.aaa.get().foo();
    }

    static class AAA {
        public AAA(BBB bbb) {
            log.debug("AAA({})", bbb.getClass().getName());
        }

        public void foo() {
            System.out.println("foo....");
        }
    }

    static class BBB {
        private Provider<AAA> aaa;

        public BBB(Provider<AAA> aaa) {
            log.debug("BBB({})", aaa.getClass().getName());
            this.aaa = aaa;
        }
    }
}
